import { BaseComponent } from './components/baseComponent';
import { ShellIntegrationService } from './services/shellIntegration';
import { AppComponents } from './components/app-components/app-components';
import { CommonComponents } from './components/common-components/common-components';
import { LDCManager } from './common/LDCManage/LDCManager';
import { employeesService } from './services/employee';
import { EmployeeInfosManage } from './services/employeesInfoManage';
import { businsessHttpServer } from './common/http/http-server';
import { MessageBoxService } from './components/messageBox/messageBoxService';
import { LocalstorageDataManage } from './common/localstoreage/localstoreageDataManage';
import { TimeManager } from './common/timeManager';
import { ExpeditionOrderMessageHandle } from './common/fnc/messageHandle';
import { MessageHandleManager } from './common/fnc/messageHandleManager';
import { FNCManager } from './common/fnc/fncManager';
import { InitializationService } from './pages/initialization/index';
import { PrividerService } from './pages/initialization/prividerService';
import { Subscription,Observable } from 'rxjs';
import { SeedDataManage } from './../assets/mock-data/seed';
import { expeditionOrdersService } from './services/expeditionOrders';
import { HomePageService } from './pages/home/index';
import { ChangeDetectionStrategy,ChangeDetectorRef ,ViewChild,ElementRef} from "@angular/core";
import { ConfirmMessageResult } from './components/messageBox/confirmMessage';
import { BaseAppComponent, SidenavOption } from './app.component';

import {
  Component,
  OnInit,
  ViewEncapsulation
} from '@angular/core';
import { Router } from "@angular/router";
import { NavigateManage } from "./common/navigateManage";

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'app',
  encapsulation: ViewEncapsulation.None,
  styleUrls: [
    './app.component.scss'
  ],
  template: `
      <div class="app-delivery-container">
          <base-app [sidenavOptions]="sidenavOptions">
              <div fxLayout="row">
                <span class="w220 text-l" fxLayoutAlign="center center">{{baseAppComponent.currentAt$ | async}}</span>
                <button class="nav-item w220 text-l color-white" fxLayoutAlign="center center" routerLink="/deliveryHome" routerLinkActive="active">待传菜 {{orderList&&orderList.length?orderList.length:0}}单</button>
                <button class="nav-item w220 text-l color-white" fxLayoutAlign="center center" routerLink="/finished" routerLinkActive="active">已完成 {{finishedList&&finishedList?finishedList.length:0}}单</button>
              </div>
          </base-app>
      </div>
  `
})
export class appCustomerComponent extends BaseComponent implements OnInit {
@ViewChild(BaseAppComponent)
private baseAppComponent:BaseAppComponent;

    sidenavOptions:Array<SidenavOption> = [
        {
          routerLink:"/setup/delivery",
          class:"iconfont icon-setting",
          title:"设置"
        },        
        {
          routerLink:"/employeeSetup/deliver",
          class:"iconfont icon-employee-setup",
          title:"员工设置"
        },        
        {
          routerLink:"/deliverySchemeSetup",
          class:"iconfont icon-deliver",
          title:"传菜方案"
        }
    ]

  private orderList;
  private finishedList;
  constructor(
    private router: Router,
    private changeDetectorRef: ChangeDetectorRef,
    private shellIntegrationService:ShellIntegrationService,
    private element: ElementRef
  ) { 
    super();
  }

  public ngOnInit() {
    this.subscriptions.push(expeditionOrdersService.getNotExpeditionOrders().subscribe(orders => {
      this.orderList = orders;
    }));
    this.subscriptions.push(expeditionOrdersService.getExpeditionOrders().subscribe(orders => {
      this.finishedList = orders;
    }));
  
    this.baseAppComponent.onInitializationServiceComplete = ()=>{
        PrividerService.current.navigate(InitializationService.current.getPathByComponentName("deliveryScheme"));
    }
}

  //mock
  addOrder() {
    const order = SeedDataManage.current.generateExpeditionOrder();
    expeditionOrdersService.addOrder(order);
  }

  async ngAfterViewInit() {
    const host = LocalstorageDataManage.current.getLDCConfig() && LocalstorageDataManage.current.getLDCConfig().host;    
    if (host) {
      await FNCManager.Current.Connect(host);
      FNCManager.Current.FNC.RegistMessageHandler("kdsMessage", (message) => {
        MessageHandleManager.Current.ReceivedMessage(message);
      })
    }

    MessageHandleManager.Current.RegistMessageHandle();
    const expeditionOrderMessageHandle = MessageHandleManager.Current.GetMessageHanle<ExpeditionOrderMessageHandle>("ExpeditionOrderessageHandle");
    expeditionOrderMessageHandle.OnMessage = async (expeditionOrder) => {
      expeditionOrdersService.addOrder(expeditionOrder);
    }
  }
}


